package com.ben.multiple.util;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

/**
 * @author Ben
 * @date 2020/01/05
 */
public final class CodeGenerator {
    public static void main(String[] args) {
        generate();
    }

    private static void generate() {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        mpg.setGlobalConfig(getGlobalConfig());
        // 数据源配置
        mpg.setDataSource(getDataSourceConfig());
        // 包配置
        mpg.setPackageInfo(getPackageConfig());
        // 策略配置
        mpg.setStrategy(getStrategyConfig());
        mpg.execute();
    }

    /**
     * @return 全局配置
     */
    private static GlobalConfig getGlobalConfig() {
        GlobalConfig gc = new GlobalConfig();
        //在当前项目下新建mybatis模块
        String projectPath = System.getProperty("user.dir") + "/mybatis";
        //设置输出文件路径
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("Ben");
        gc.setOpen(false);
        String clazzName = "%s";
        //设置类名
        gc.setEntityName(clazzName);
        gc.setControllerName(clazzName + "Resource");
        gc.setServiceName(clazzName + "Service");
        gc.setServiceImplName(clazzName + "ServiceImpl");
        gc.setMapperName(clazzName + "Mapper");
        gc.setXmlName(clazzName + "Mapper");
        return gc;
    }

    /**
     * @return 数据源配置
     */
    private static DataSourceConfig getDataSourceConfig() {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/sys_mini?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        return dsc;
    }

    /**
     * @return 包配置
     */
    private static PackageConfig getPackageConfig() {
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(scanner("模块名"));
        pc.setParent("com.ben");
        pc.setEntity("api.entity");
        pc.setService("biz.service");
        pc.setServiceImpl("biz.service.impl");
        pc.setController("biz.rest");
        pc.setMapper("biz.mapper");
        return pc;
    }

    /**
     * @return 策略配置
     */
    private static StrategyConfig getStrategyConfig() {
        StrategyConfig strategy = new StrategyConfig();
        //表名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //字段名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //超类
        strategy.setSuperEntityClass("com.ben.gmsf.demo.mybatis.BaseEntityModel");
        //使用lombok
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //输入表名，多个表用逗号分隔
        // sys_user,sys_user_role,sys_role,sys_role_menu,sys_role_button,sys_menu,sys_button
        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
        //超类中的字段
        strategy.setSuperEntityColumns("id", "create_time", "update_time");
        strategy.setControllerMappingHyphenStyle(true);
        return strategy;
    }

    /**
     * @param tip 提示
     * @return 读取控制台内容
     */
    private static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        return scanner.next();
    }
}
